package com.vieewer.btdemo.view.brightness

import android.media.AudioManager
import android.os.Bundle
import android.widget.SeekBar
import com.alibaba.fastjson.JSON
import com.vieewer.btdemo.R
import com.vieewer.btdemo.base.BaseActivity
import com.vieewer.btdemo.bean.SystemSettingsBean
import com.vieewer.btdemo.bean.SystemSettingsBean.SYS_TYPE_BRIGHTNESS
import com.vieewer.btdemo.bean.SystemSettingsBean.SYS_TYPE_VOLUME
import com.vieewer.btdemo.utils.CommunicateToGlassesHelper
import com.vieewer.classicbt.CLog
import kotlinx.android.synthetic.main.activity_brightness.sbSystemBright
import kotlinx.android.synthetic.main.activity_brightness.tvSystemBright
import kotlinx.android.synthetic.main.activity_set_glasses_bright.*

/*手机端调节眼镜端亮度*/
class SettingsActivity :  BaseActivity() {
    //调节亮度进度
    var mProgressValue = 0
    //调节系统音量相关
    private var mAudioManager: AudioManager? = null
    private var mVolumeProgress = 7

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_set_glasses_bright)
        initBrightness()
        initVolume()
        
    }

    private fun initBrightness() {
        sbSystemBright.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar?, progress: Int, fromUser: Boolean) {
                mProgressValue = progress * 255 / 100
                //systemBrightness = progress * 255 / 100
                tvSystemBright.text = "系统亮度=$mProgressValue"

                CLog.i("onProgressChanged >>>" + mProgressValue.toString())
            }


            override fun onStartTrackingTouch(seekBar: SeekBar?) {
                CLog.i("onStartTrackingTouch >>>")
            }


            override fun onStopTrackingTouch(seekBar: SeekBar?) {
                CLog.i("onStopTrackingTouch brightness >>>")
                var data = SystemSettingsBean()
                data.sys_type = SYS_TYPE_BRIGHTNESS
                data.progress = mProgressValue
                CommunicateToGlassesHelper.instance.write(JSON.toJSONString(data))
            }

        })
    }

    private fun initVolume() {
        mAudioManager = getSystemService(AUDIO_SERVICE) as AudioManager
        //系统最大音量为15
        sbSystemVolume.max = mAudioManager!!.getStreamMaxVolume(AudioManager.STREAM_MUSIC)
        mAudioManager!!.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeProgress, AudioManager.FLAG_SHOW_UI)
        sbSystemVolume.progress = mVolumeProgress
        txt_volume.text = "" + mVolumeProgress

        sbSystemVolume.setOnSeekBarChangeListener(object : SeekBar.OnSeekBarChangeListener {
            override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
                mVolumeProgress = progress
                txt_volume.text = "" + progress

            }

            override fun onStartTrackingTouch(seekBar: SeekBar) {

            }

            override fun onStopTrackingTouch(seekBar: SeekBar) {
                var data = SystemSettingsBean()
                data.sys_type = SYS_TYPE_VOLUME
                data.progress = mVolumeProgress
                CommunicateToGlassesHelper.instance.write(JSON.toJSONString(data))
            }
        })

    
    }



/*
    //手机音量+-按钮监听
    override fun onKeyDown(keyCode: Int, event: KeyEvent?): Boolean {

        when (keyCode) {
            KeyEvent.KEYCODE_VOLUME_DOWN -> {
                if (mVolumeProgress == 0) {
                    mVolumeProgress = 0
                    return true
                }
                --mVolumeProgress
                mAudioManager!!.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeProgress, AudioManager.FLAG_SHOW_UI)
                txt_volume.text = "" + mVolumeProgress
                sbSystemVolume.progress = mVolumeProgress
                return true
            }
            KeyEvent.KEYCODE_VOLUME_UP -> {
                if (mVolumeProgress == 15) {
                    mVolumeProgress = 15
                    return true
                }
                ++mVolumeProgress
                mAudioManager!!.setStreamVolume(AudioManager.STREAM_MUSIC, mVolumeProgress, AudioManager.FLAG_SHOW_UI)
                txt_volume.text = "" + mVolumeProgress
                sbSystemVolume.progress = mVolumeProgress
                return true
            }
        }
        return super.onKeyDown(keyCode, event)

    }
*/



}